<template>
  <a-card :bordered="false" class="user-panel-card">
    <div class="user-panel">
      <div class="user-info-section">
        <a-upload
          list-type="picture-card"
          :file-list="fileList"
          :show-file-list="false"
          @progress="upload"
        >
          <template #upload-button>
            <div class="avatar-container">
              <a-avatar :size="120" class="info-avatar">
                <img v-if="fileList.length" :src="fileList[0].url" />
                <template #trigger-icon>
                  <div class="edit-icon">
                    <icon-edit />
                  </div>
                </template>
              </a-avatar>
            </div>
          </template>
        </a-upload>

        <!-- <div class="user-name">
          <h3>{{ userStore.realName || '未设置姓名' }}</h3>
          <p class="user-role">学生</p>
        </div> -->
      </div>

      <div class="user-details">
        <div class="detail-row">
          <div class="detail-item">
            <span class="item-label">账户ID</span>
            <span class="item-value">{{ userStore.id || '-' }}</span>
          </div>
          <div class="detail-item">
            <span class="item-label">手机号</span>
            <span class="item-value">
              {{ formatPhone(userStore.mobile) || '-' }}
            </span>
          </div>
        </div>
        <div class="detail-row">
          <div class="detail-item">
            <span class="item-label">邮箱</span>
            <span class="item-value">{{ userStore.email || '-' }}</span>
          </div>
          <div class="detail-item">
            <span class="item-label">注册日期</span>
            <span class="item-value">
              {{ formatDate(userStore.registerTime) || '-' }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </a-card>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import type { FileItem } from '@arco-design/web-vue/es/upload/interfaces';
import { useUserStore } from '@/store';
import { SysUser, SysUserService } from '@/api/gen-api';
import { Message } from '@arco-design/web-vue';

const userStore = useUserStore();
const file = {
  uid: '-2',
  name: 'avatar.png',
  url: userStore.avatar
};

const fileList = ref<FileItem[]>([file]);

// 格式化电话号码，保留前三位和后四位，中间用星号代替
const formatPhone = (phone: string | undefined): string => {
  if (!phone || phone.length < 7) return phone || '';
  return `${phone.substring(0, 3)}****${phone.substring(phone.length - 4)}`;
};

// 格式化日期，只显示年月日
const formatDate = (dateString: string | undefined): string => {
  if (!dateString) return '';
  try {
    const date = new Date(dateString);
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    });
  } catch (e) {
    return dateString;
  }
};

const upload = (fileItem: FileItem) => {
  fileList.value = [fileItem];

  // 显示加载状态
  const loadingMessage = Message.loading({
    content: '正在上传头像...',
    duration: 0
  });

  SysUserService.upload({
    file: fileList.value[0].file
  })
    .then(res => {
      // 关闭加载消息
      loadingMessage.close();

      if (res.result) {
        Message.success('头像上传成功');
        // 更新store中的头像，通过setInfo方法更新整个用户信息对象
        userStore.setInfo({
          ...userStore.userInfo,
          avatar: res.result
        });
      } else {
        Message.error('头像上传失败');
      }
    })
    .catch(() => {
      loadingMessage.close();
      Message.error('上传失败，请稍后重试');
    });
};

onMounted(() => {
  SysUserService.getInfo().then(res => {
    if (res.result) {
      userStore.setInfo(res.result);
      // 更新头像地址
      fileList.value = [
        {
          uid: '-2',
          name: 'avatar.png',
          url: res.result.avatar || userStore.avatar
        }
      ];
    }
  });
});
</script>

<style scoped lang="less">
.user-panel-card {
  box-shadow: none;

  :deep(.arco-card-body) {
    padding: 24px;
  }
}

.user-panel {
  display: flex;
  flex-direction: column;
  gap: 24px;

  @media (min-width: 768px) {
    flex-direction: row;
    align-items: center;
  }
}

.user-info-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;

  @media (min-width: 768px) {
    flex-direction: row;
    gap: 24px;
  }
}

.avatar-container {
  position: relative;

  :deep(.arco-avatar) {
    border: 4px solid rgba(39, 89, 205, 0.1);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
    transition:
      transform 0.3s ease,
      box-shadow 0.3s ease;

    &:hover {
      transform: scale(1.02);
      box-shadow: 0 6px 20px rgba(39, 89, 205, 0.15);
    }
  }

  :deep(.arco-avatar-trigger-icon-button) {
    width: 36px;
    height: 36px;
    line-height: 36px;
    background-color: #2759cd;
    border: 3px solid #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: transform 0.3s ease;

    &:hover {
      transform: rotate(15deg);
    }

    .edit-icon {
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;

      .arco-icon {
        font-size: 16px;
      }
    }
  }
}

.user-name {
  text-align: center;

  @media (min-width: 768px) {
    text-align: left;
  }

  h3 {
    font-size: 20px;
    font-weight: 600;
    color: var(--color-text-1);
    margin: 0 0 6px 0;
  }

  .user-role {
    font-size: 14px;
    color: var(--color-text-3);
    margin: 0;
  }
}

.user-details {
  flex: 1;
  background-color: var(--color-fill-2);
  border-radius: 8px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;

  @media (min-width: 768px) {
    margin-left: 24px;
  }

  .detail-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;

    @media (min-width: 576px) {
      flex-wrap: nowrap;
    }
  }

  .detail-item {
    flex: 1;
    min-width: 200px;
    display: flex;
    flex-direction: column;
    gap: 4px;

    .item-label {
      font-size: 14px;
      color: var(--color-text-3);
    }

    .item-value {
      font-size: 15px;
      font-weight: 500;
      color: var(--color-text-1);
    }
  }
}
</style>
